/*
 * Copyright 2016 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mingmen.canting.caigourenyuan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mingmen.canting.R;
import com.mingmen.canting.adapter.JuBaoAdapter;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.immersionbar.ImmersionBar;
import com.mingmen.canting.model.JuBaoModel;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;


/**
 * <p>
 * ListView形式的侧滑菜单，支持横向和竖向的。
 * </p>
 * Created by Yan Zhenjie on 2016/7/27.
 */
public class JuBaoShangJiaActivity extends Activity {

    @BindView(R.id.text_return)
    TextView textReturn;
    @BindView(R.id.linear_return)
    LinearLayout linearReturn;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.img_right)
    ImageView imgRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.mToolbar)
    Toolbar mToolbar;
    @BindView(R.id.btn_add_address)
    Button btnRegister;
    @BindView(R.id.lable_selectcanting)
    LinearLayout lableSelectcanting;


    @BindView(R.id.layout_all)
    RelativeLayout layoutAll;

    String token;

    @BindView(R.id.recycle_shangjia)
    RecyclerView recycle_shangjia;

    LinearLayoutManager linearLayoutManager;
    JuBaoAdapter juBaoAdapter;
    List<JuBaoModel.DataBean> stringList = new ArrayList<>();

    String codeId;
    String codeName;
    String flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jubaoshangjia);
        ButterKnife.bind(this);
        textCenter.setText("举报商家");
        token = (String) SharePreferenceUtil.get(this, "token", "");
        ImmersionBar.with(this).titleBar(mToolbar)
                .navigationBarColor(R.color.black).init();
        linearLayoutManager = new LinearLayoutManager(this);
        recycle_shangjia.setLayoutManager(linearLayoutManager);
        juBaoAdapter = new JuBaoAdapter(this, stringList);
        recycle_shangjia.setAdapter(juBaoAdapter);
        flag = getIntent().getStringExtra("flag");
        HttpResponse.getReportTypeList(token, new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                JuBaoModel juBaoModel = new JuBaoModel();
                juBaoModel = GsonUtil.GsonToBean(s, JuBaoModel.class);

                stringList.addAll(juBaoModel.getData());
                juBaoAdapter.setDataList(stringList);
            }
        });

        juBaoAdapter.setOnItemClickListener(new JuBaoAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int pos, List<JuBaoModel.DataBean> myLiveList) {
//                Toast.makeText(JuBaoShangJiaActivity.this, myLiveList.get(pos).getCodeId() + "", Toast.LENGTH_SHORT).show();
                switch (view.getId()) {
                    case R.id.checkBoxs:
                    default:
                        myLiveList.get(pos).setSelect(true);
                        codeId = myLiveList.get(pos).getCodeId();
                        codeName = myLiveList.get(pos).getCodeName();
                        juBaoAdapter.setCheckedPosition(pos);
                        break;
                }


            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


    @OnClick({R.id.text_return, R.id.linear_return, R.id.btn_add_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_return:
                finish();
                break;
            case R.id.linear_return:
                finish();
                break;
            case R.id.btn_register:
                break;
            case R.id.btn_add_address:
                if (TextUtils.isEmpty(codeId)) {
                    Toast.makeText(this, "请选择举报商家", Toast.LENGTH_SHORT).show();
                } else {
                    if (flag.equals("1")) {
                        Intent intent = new Intent(JuBaoShangJiaActivity.this, JuBaoActivity.class);
                        intent.putExtra("codeId", codeId);
                        intent.putExtra("codeName", codeName);

                        setResult(100, intent);
                        finish();
                    } else {
                        Intent intent = new Intent(JuBaoShangJiaActivity.this, JuBaoActivity.class);
                        intent.putExtra("codeId", codeId);
                        intent.putExtra("codeName", codeName);

                        setResult(100, intent);
                        finish();
                    }

                }


                break;
        }
    }


}